#ifndef JTHREAD_P_H
#define JTHREAD_P_H

// W A R N I N G
// -------------
// This file is not JXQLIB-APIs

#include <JXQCore/JMutex>
#include <JXQCore/JDebug>
#include <condition_variable>
#include <atomic>

#ifdef J_OS_WIN
# include <windows.h>
#endif

namespace JXQ
{

class JThread;

class J_DECL_HIDDEN JThreadPrivate
{
private:
	explicit JThreadPrivate(JThread *q_ptr);

private:
#ifdef J_OS_WIN
	std::atomic<HANDLE> m_handle {nullptr};
	int m_stackSize = 128 * 1024;
#elif defined(J_OS_UNIX) || defined(J_OS_LINUX)
	std::atomic<pthread_t> m_tid {0};
	// uinx/linux的线程与进程使用同一个结构体(task)，
	// 线程的默认堆栈大小 = (进程堆栈/2  <= size <= 进程堆栈)
#endif

private:
	JThread *q_ptr;
	std::mutex m_conditionMutex;
	std::condition_variable m_condition;
	std::atomic_int m_ref {1}; //计数器，记录谁在占用这个类

	bool m_isDetach = false;
	bool m_deleteOnFinished = false;

private:
#ifdef J_OS_WIN
	static
# ifdef __GNUC__
	WINAPI
# endif
	DWORD run(LPVOID lpThreadParameter);
#elif defined(J_OS_UNIX) || defined(J_OS_LINUX)
	static void *run(void *arg);
#endif

	void finish();

	friend class JThread;
};

} //namespace JXQ


#endif //JTHREAD_P_H
